from django.template import loader, RequestContext
from django.http import HttpResponse
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from duetopia.register.models import Layer, DataFormat

__all__ = ['view_layer', 'view_thumb']

def view_layer(request, pk, template='register/view_layer.html'):
	layer = get_object_or_404(Layer, pk=pk)
	context = {
		'layer' : layer,
		'tilecache': settings.TILECACHE,
	}
	format = DataFormat.objects.get(app="register",model="WebMapService")
	sources = layer.dataset.datasource_set.filter(format = format)
	if sources.count():
		context['wms'] = sources[0]
	c = RequestContext(request, context)
	t = loader.get_template(template)
	return HttpResponse(t.render(c))

def view_thumb(request, pk):
	layer = get_object_or_404(Layer, pk=pk)
	try:
		thumbnail = layer.thumbnail
	except:
		raise Http404('no thumbnail')
	return HttpResponse(thumbnail, mimetype=layer.thumbnail_format.mimetype)
